<template>
    <div>
        <h1>新增员工</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">标题</td>
                    <td>
                        <input type="text" v-model="info.Htitle">
                    </td>
                </tr>
                                <tr>
                    <td align="right">图片</td>
                    <td>
                        <input type="file" @change="imgs">
                        <img :src="info.ImgUrl" width="100" height="100">
                    </td>
                </tr>
                                <tr>
                    <td align="right">分类</td>
                    <td>
                        <select v-model="info.Did">
                            <option value="">请选择</option>
                            <option :value="item.Did" v-for="item in gettype">{{ item.Dname }}</option>
                        </select>
                    </td>
                </tr>
                                <tr>
                    <td align="right">日期</td>
                    <td>
                        <input type="date" v-model="info.Hdate">
                    </td>
                </tr>
                <tr>
                    <td align="right">状态</td>
                    <td>
                        <input type="radio" :value="true" name="state" v-model="info.Hstate">已完成
                        <input type="radio" :value="false" name="state" v-model="info.Hstate">未完成
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="修改" class="btn btn-success" @click="updateuse">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from 'vue'
import axios from 'axios'
import { useRoute,useRouter } from 'vue-router';
import moment from 'moment';
const router = useRouter();
const route = useRoute();

let info:any=reactive({
  "Htitle": "",
  "Did": "",
  "ImgUrl": "",
  "Hdate": "",
  "Hstate": ""
})
onMounted(()=>{
    GetTypeXl();
})

const FanHos=(id:any)=>{
    axios({
        url:"/api/Use/FanHuo",
        method:"get",
        params:{
            id:id
        }
    })
    .then((res)=>{
        console.log(res);
        Object.assign(info,res.data);
        info.Hdate = res.data.Hdate.substring(0,10);
    })
}

let gettype:any=ref([]);

const GetTypeXl=()=>{
    axios({
        url:"/api/Use/GetDeus",
        method:"get"
    })
    .then((res)=>{
        console.log(res);
        gettype.value = res.data;
    })
    .catch((err)=>{
        console.log(err);
    })
}

const imgs=(e:any)=>{
    let obj = e.format.files[0];
    let f = new FormData();
    f.append("file",obj);
    axios({
        url:"/api/Img/Imgs",
        method:"post",
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="大于2M")
    {
        alert("您上传图片大于2M");
        return;
    }
    info.ImgUrl = res.data;
    alert("上传成功");
    })
    .catch((err)=>{
        console.log(err);
    })
}

const updateuse=()=>{
    if(info.Htitle=="")
{
    alert("标题不能为空");
    return;
}
    if(info.Did=="")
{
    alert("分类不能为空");
    return;
}
    if(info.Tid=="")
{
    alert("日期不能为空");
    return;
}
    axios({
        url:"/api/Use/UpdateHuo",
        method:"put",
        data:info
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0)
    {
        alert("修改成功");
        router.push("/UpdateHose");
        return;
    }
    else if(res.data==-1)
    {
        alert("标题不能重复");
        return;
    }
    else
    {
        alert("修改失败");
        return;
    }
    })
    .catch((err)=>{
        console.log(err);
    })
}
</script>

<style scoped>

</style>